package com.dxx.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxx.cloud.admin.constant.CommonConst;
import com.dxx.cloud.admin.mapper.OrderDetailMapper;
import com.dxx.cloud.admin.module.entity.GoodsSizeEntity;
import com.dxx.cloud.admin.module.entity.OrderDetailEntity;
import com.dxx.cloud.admin.module.response.OrderDetailResponse;
import com.dxx.cloud.admin.service.IGoodsSizeService;
import com.dxx.cloud.admin.service.IOrderDetailService;
import com.dxx.cloud.admin.utils.EntityUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 订单详情表 服务实现类
 * </p>
 *
 * @author dh
 * @since 2025-06-06
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetailEntity> implements IOrderDetailService {

    @Resource
    private IGoodsSizeService goodsSizeService;

    @Override
    public List<OrderDetailEntity> listByOrderId(Serializable orderId) {
        if(null ==orderId) {
            return List.of();
        }
        return list(new LambdaQueryWrapper<OrderDetailEntity>().eq(OrderDetailEntity::getOrderId, orderId));
    }

    @Override
    public List<OrderDetailResponse> listResponseByOrderId(Serializable orderId, Integer orderStatus) {
        final List<OrderDetailEntity> orderDetailEntities = listByOrderId(orderId);
        final List<OrderDetailResponse> orderDetailResponses = EntityUtils.entityList2ResponseList(OrderDetailResponse.class, orderDetailEntities);
        if (!CommonConst.OrderPayStatusEnum.PAYED.getCode().equals(orderStatus)) {
            orderDetailResponses.forEach(orderDetailResponse -> {
                // 订单未支付时，从商品规格获取信息
                // 订单已支付时，从订单详情获取
                final GoodsSizeEntity byId = goodsSizeService.getById(orderDetailResponse.getGoodsSizeId());
                if(null != byId) {
                    BeanUtils.copyProperties(byId, orderDetailResponse);
                }
            });
        }
        return orderDetailResponses;
    }
}
